package cn.k23.controller;

import cn.k23.mapper.CarouselMapper;
import cn.k23.mapper.PermissionMapper;
import cn.k23.mapper.UserLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: k23
 * @Date: 2022-03-29 16:04
 * @Version: 1.0
 */
@Controller
public class IndexController {
    @RequestMapping
    public String index(Model m){
        m.addAttribute("msg","我是卢本伟");
        return "index";
    }

    @RequestMapping("/login")
    public String login(Model m){
        return "login";
    }

    @Autowired
    private UserLogMapper ulm;
    @RequestMapping("/home")
    public String home(Model m,HttpSession s){
        //m.addAttribute("logs",ulm.queryAll());
        m.addAttribute("banners",cm.show());
        m.addAttribute("logs",ulm.query(s.getAttribute("user").toString()));
        return "home";
    }

    @Autowired
    private PermissionMapper pm;

    @GetMapping("/init")
    @ResponseBody
    public Map<String,Object> init(HttpSession s){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> m1 = new HashMap<>();
        m1.put("title","首页");
        m1.put("href","home");
        map.put("homeInfo",m1);

        Map<String,String> m2 = new HashMap<>();
        m2.put("title","碳积分管理系统");
        m2.put("image","images/logo.png");
        m2.put("href","home");
        map.put("logoInfo",m2);

        Map<String,Object> menu = new HashMap<>();
        menu.put("title","默认左侧菜单");
        menu.put("icon","fa fa-address-book");
        menu.put("href","javascript:void(0)");
        menu.put("target","_self");
        menu.put("child",pm.menu(s.getAttribute("user").toString()));
        List<Map<String,Object>> menus = new ArrayList<>();
        menus.add(menu);
        map.put("menuInfo",menus);
        return map;
    }



    @Autowired
    private CarouselMapper cm;
    @RequestMapping("/carousel")
        public String carousel(Model m){
            m.addAttribute("banners",cm.queryAll());
            return "carousel";
        }


    @GetMapping("api/carouseldb")
    @ResponseBody
    public Map<String,Object> carouseldb(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",4);
        map.put("data",cm.queryAll());
        return map;
    }


}
